package com.runen.infinitemovement.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.WebActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.PathSectionCompleteEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.popupwind.SharePoppupWindow;

import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class PathSectionCompleteAdapter extends BaseMultiItemQuickAdapter<PathSectionCompleteEntity.DataBean, BaseViewHolder> {
    private Context context;
    public static final int TYPE_COMPLETE = 0;
    public static final int TYPE_MEDAL = 1;
    public static final int TYPE_PRIZE = 2;

    private Click click;

    public PathSectionCompleteAdapter(Context context, List<PathSectionCompleteEntity.DataBean> list, Click click) {
        super(list);
        this.context = context;
        this.click = click;
        //设置当传入的itemType为某个常量显示不同的item
        addItemType(TYPE_COMPLETE, R.layout.item_path_section_complete);
        addItemType(TYPE_MEDAL, R.layout.item_path_section_medal);
        addItemType(TYPE_PRIZE, R.layout.item_path_section_prize);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, PathSectionCompleteEntity.DataBean dataBean) {
        LinearLayout llContent = baseViewHolder.findView(R.id.ll_content);
        RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) llContent.getLayoutParams();
        int width = CommonUtil.getScreenHW((Activity) context)[0];
        int llContentWidth = (int) (width * 0.72);
        lp.width = llContentWidth;
        int leftMargin = (width - llContentWidth) / 2;
        if (getData().size() == 1) {
            lp.leftMargin = leftMargin;
            lp.rightMargin = leftMargin;
        } else if (getData().size() == 2) {
            if (baseViewHolder.getAdapterPosition() == 0) {
                lp.leftMargin = leftMargin;
                lp.rightMargin = (int) (leftMargin * 0.384);
            } else {
                lp.leftMargin = (int) (leftMargin * 0.384);
                lp.rightMargin = leftMargin;
            }
        } else {
            if (baseViewHolder.getAdapterPosition() == 0) {
                lp.leftMargin = leftMargin;
                lp.rightMargin = (int) (leftMargin * 0.384);
            } else if (baseViewHolder.getAdapterPosition() == 1) {
                lp.leftMargin = (int) (leftMargin * 0.384);
                lp.rightMargin = (int) (leftMargin * 0.384);
            } else {
                lp.leftMargin = (int) (leftMargin * 0.384);
                lp.rightMargin = leftMargin;
            }
        }
        llContent.setLayoutParams(lp);
        switch (baseViewHolder.getItemViewType()) {
            case TYPE_COMPLETE:
                TextView tv_rank = baseViewHolder.findView(R.id.tv_rank);
                TextView tv_start_end_name = baseViewHolder.findView(R.id.tv_start_end_name);
                TextView tv_hozom = baseViewHolder.findView(R.id.tv_hozom);
                TextView tv_share = baseViewHolder.findView(R.id.tv_share);

                tv_rank.setText(dataBean.myRank + "");
                String startEndName = "完成 <font color='#B7232B'>%s-%s<font/> 路段的人";
                tv_start_end_name.setText(Html.fromHtml(String.format(startEndName, dataBean.startPoint, dataBean.endPoint)));
                String huozom = "获得火种：%s颗";
                tv_hozom.setText(String.format(huozom, dataBean.tinderNumber));
                tv_share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (click != null)
                            click.shareClick();
                    }
                });
                break;
            case TYPE_MEDAL:
                TextView tv_medal = baseViewHolder.findView(R.id.tv_medal);
                ImageView iv_banner_image = baseViewHolder.findView(R.id.iv_banner_image);
                GlideUtils.loadImage(context, dataBean.sectionMedalUrl, iv_banner_image);
                String medal = "获得【%s-%s】徽章";
                tv_medal.setText(String.format(medal, dataBean.startPoint, dataBean.endPoint));
                break;
            case TYPE_PRIZE:
                TextView tv_message = baseViewHolder.findView(R.id.tv_message);
                ImageView iv_content = baseViewHolder.findView(R.id.iv_content);
                TextView tv_prize_name = baseViewHolder.findView(R.id.tv_prize_name);
                TextView tv_receive_award = baseViewHolder.findView(R.id.tv_receive_award);
                String message = "%s-%s";
                tv_message.setText(String.format(message, dataBean.startPoint, dataBean.endPoint));
                GlideUtils.loadImage(context, dataBean.prizeUrl, iv_content);
                String name = "【%s】";
                if (dataBean.prizeName.length() > 6) {
                    dataBean.prizeName = dataBean.prizeName.substring(0, 5) + "...";
                }
                tv_prize_name.setText(String.format(name, dataBean.prizeName));

                tv_receive_award.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent1 = new Intent(context, WebActivity.class);
                        intent1.putExtra("LINK_URL", String.format(Constant.H5_RECEIVE_AWARD,
                                UserAPI.getUserToken(), dataBean.pathId, dataBean.sectionId));
                        context.startActivity(intent1);
                    }
                });
                break;
        }

    }

    public interface Click {
        void shareClick();
    }
}
